<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('题库管理列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>题目类型：</label>
                                <select name="questionType" th:with="type=${@dict.getType('question_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>难度：</label>
                                <select name="difficulty" th:with="type=${@dict.getType('difficulty')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>教材页码：</label>
                                <input type="text" name="pageNumber"/>
                            </li>
                            <li>
                                <label>来源：</label>
                                <input type="text" name="source"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

<!--            <div class="btn-group-sm" id="toolbar" role="group">-->
<!--                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="question:questions:add">-->
<!--                    <i class="fa fa-plus"></i> 添加-->
<!--                </a>-->
<!--                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="question:questions:edit">-->
<!--                    <i class="fa fa-edit"></i> 修改-->
<!--                </a>-->
<!--                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="question:questions:remove">-->
<!--                    <i class="fa fa-remove"></i> 删除-->
<!--                </a>-->
<!--            </div>-->
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('question:questions:edit')}]];
        var removeFlag = [[${@permission.hasPermi('question:questions:remove')}]];
        var questionTypeDatas = [[${@dict.getType('question_type')}]];
        var difficultyDatas = [[${@dict.getType('difficulty')}]];
        var prefix = ctx + "question/questions";
        var listId = [[${listId}]];

        $(function() {
            var options = {
                url: prefix + "/detailsList/" + listId,
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "题库管理",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键UUID',
                    visible: false
                },
                {
                    field: 'questionType',
                    title: '题目类型',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(questionTypeDatas, value);
                    }
                },
                {
                    field: 'questionName',
                    title: '题目',
                    formatter: function(value, row, index) {
                        // 检查值是否为空
                        if (value) {
                            // 使用 $('<div>').text() 转换 HTML 字符串为纯文本
                            return $('<div>').html(value).text();
                        }
                        return value;
                    }
                },
                {
                    field: 'answer',
                    title: '答案',
                    formatter: function(value, row, index) {
                        // 检查值是否为空
                        if (value) {
                            // 使用 $('<div>').text() 转换 HTML 字符串为纯文本
                            return $('<div>').html(value).text();
                        }
                        return value;
                    }
                },
                {
                    field: 'correctAnswer',
                    title: '正确答案',
                    formatter: function(value, row, index) {
                        // 检查值是否为空
                        if (value) {
                            // 使用 $('<div>').text() 转换 HTML 字符串为纯文本
                            return $('<div>').html(value).text();
                        }
                        return value;
                    }
                },
                {
                    field: 'difficulty',
                    title: '难度',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(difficultyDatas, value);
                    }
                },
                {
                    field: 'pageNumber',
                    title: '教材页码'
                },
                {
                    field: 'source',
                    title: '来源'
                },
                {
                    field: 'note',
                    title: '备注'
                },
                {
                    field: 'solution',
                    title: '解题思路'
                }
                ],
                responseHandler: function(res) {
                    var rows = res.rows || [];
                    rows.forEach(function(row, index) {
                        if (row.backupField1 === "1") {
                            row.state = true; // 设置选中状态
                            // 延迟执行选中操作，确保表格已初始化
                            setTimeout(function() {
                                $('#bootstrap-table').bootstrapTable('check', index); // 选中行
                            }, 0);
                        }
                    });
                    return {
                        rows: rows, // 返回修改后的数据
                        total: res.total // 保持分页信息
                    };
                }
            };
            $.table.init(options);
        });

        // 监听表格的选中事件
        $('#bootstrap-table').on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, rows) {
            // 调用 saveSelectedData 方法，保存当前选中的数据
            saveSelectedData();
        });

        function saveSelectedData() {
            var selectedRows = $('#bootstrap-table').bootstrapTable('getSelections');

            // 存储选中的数据到 LocalStorage
            var ids = selectedRows.map(function(row) {
                return row.id;
            });
            localStorage.setItem('selectedIds', JSON.stringify(ids));  // 将 ID 数组存入 LocalStorage
        }
    </script>
</body>
</html>